package com.sxz.sbs.module.sms;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.event.ChoseSmsClientEvent;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.sms.adapter.SmsClientAdapter;
import com.sxz.sbs.module.sms.mode.SmsClientBean;
import com.sxz.sbs.module.sms.presenter.SmsClientPresenter;
import com.sxz.sbs.module.sms.view.SmsClientView;
import com.sxz.sbs.widget.CommonTitle;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 发短信 选择客户列表
 */
public class SmsClientActivity extends BaseActivity<SmsClientPresenter> implements SmsClientView {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private List<SmsClientBean> data = new ArrayList<>();
    private SmsClientAdapter adapter;
    @Override
    protected void init() {
        new CommonTitle.Builder(this).setTitle("客户列表").build().show();
        EventBus.getDefault().register(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });
        refreshLayout.setRefreshing(true);
        getData();
        adapter = new SmsClientAdapter(data);
        recyclerView.setAdapter(adapter);
        stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
            @Override
            public void onRetry(int state) {
                getData();
            }
        });
        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(SmsClientActivity.this, ChoseSmsClientActivity.class)
                        .putExtra("client_type", data.get(position).getType()).
                                putExtra("client_txt", data.get(position).getName()));
            }
        });

    }

    /**
     * 获取客户列表类型
     */
    public void getData() {
        HashMap<String, Object> map = HttpParamMap.getParamMap();
        mPresenter.getSmsClientList(map);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sms_client;
    }

    @Override
    protected SmsClientPresenter createPresenter() {
        return new SmsClientPresenter();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFinishChoseEvent(ChoseSmsClientEvent event) {
        finish();
    }

    @Override
    public void onGetClientNumSuccess(List<SmsClientBean> bean) {
        if (mIsLive ) {
            refreshLayout.setRefreshing(false);
            if (bean != null) {
                stateLayout.showContent();
                data.clear();
                data.addAll(bean);
                adapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Override
    public void loginAgain() {
        super.loginAgain();
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void showError(int errorTag, String msg) {
        refreshLayout.setRefreshing(false);
        if (errorTag == AppConstant.NET_ERROR_CODE) {
            stateLayout.showNetwork();
        } else {
            stateLayout.showError();
        }
    }

}
